package com.qcln.dao;

import com.qcln.bean.Student;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

public class StudentTest01 {

    @Test
    public void selectAll() throws Exception{
        // 1. 加载核心配置文件
//        InputStream inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        InputStream inputStream = StudentTest01.class.getClassLoader().getResourceAsStream("MyBatisConfig.xml");


        // 2. 获取sqlSession工场对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);

        // 3. 通过sqlSession工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession();

        // 4. 执行映射配置文件中的sql语句，并接受结果
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        // 5. 处理结果
        for (Student student : list) {
            System.out.println(student);
        }

        // 6. 释放资源
        sqlSession.close();
        inputStream.close();

    }

    // 根据id查询
    @Test
    public void selectById() throws Exception{
        // 1. 加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        // 2. 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);

        // 3. 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession();

        // 4. 执行映射配置文件中的sql语句，并接收结果
        Student stu = sqlSession.selectOne("StudentMapper.selectById", 3);

        // 5. 处理结果
        System.out.println(stu);

        // 6. 释放资源
        sqlSession.close();
        resourceAsStream.close();
    }

    // 新增功能
    @Test
    public void insert() throws Exception{
        // 1. 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 2. 获取SqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        // 3. 通过SqlSession工厂对象获取SqlSession对象,默认是不自动提交事务的
        SqlSession sqlSession = build.openSession();
//        SqlSession sqlSession = build.openSession(true);自动提交事务

        // 4. 执行映射配置文件中的sql语句，并接收结果
        Student stu = new Student(4,"赵六",26);
        int insert = sqlSession.insert("StudentMapper.insert",stu);

        // 5手动提交事务
        sqlSession.commit();

        // 6. 处理结果
        System.out.println(insert);

        // 7. 释放资源
        sqlSession.close();
        is.close();
    }

    // 修改功能
    @Test
    public void update() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        Student stu = new Student(4,"赵六",36);
        int i = sqlSession.update("StudentMapper.update", stu);
        sqlSession.commit();
        System.out.println(i);
        sqlSession.close();
        is.close();
    }

    // 删除功能
    @Test
    public void delete() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        int i = sqlSession.delete("StudentMapper.delete",4);
        sqlSession.commit();
        System.out.println(i);
        sqlSession.close();
        is.close();

    }

}
